package priv.pfz.qatool.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by yyjxp on 2021/4/17.
 */
public class ParseUtil {
    private static SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static long parseTime(String time) throws ParseException {
        if (StringUtils.isEmpty(time)) {
            return 0L;
        }
        return DATE_TIME_FORMAT.parse(time).getTime();
    }

    public static long parseDate(String date) throws ParseException {
        if (StringUtils.isEmpty(date)) {
            return 0L;
        }
        return DATE_FORMAT.parse(date).getTime();
    }

    public static <T> T parseString(String value, Class<T> clazz) {
        if (clazz == String.class) {
            return (T) value;
        } else if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(value);
        } else if (clazz == double.class || clazz == Double.class) {
            return (T) Double.valueOf(value);
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(value);
        } else if (clazz == short.class || clazz == Short.class) {
            return (T) Short.valueOf(value);
        } else if (clazz == float.class || clazz == Float.class) {
            return (T) Float.valueOf(value);
        } else if (clazz == boolean.class || clazz == Boolean.class) {
            return (T) Boolean.valueOf(value);
        } else if (clazz == byte.class || clazz == Byte.class) {
            return (T) Byte.valueOf(value);
        }
        throw new IllegalArgumentException();
    }
}
